Scripting: Kill Brick (Explosive)
This scripting tutorial will teach you how to create a brick in Roblox that will generate an explosion and kill the player when they step upon it. The brick First, you will need to create a brick by going into Insert, then Object, then select Part. The part will then appear in Workspace. It is then recommended you set this part's Anchored value to true by either using the Anchor tool, or ticking the "Anchored" box in the Properties if you have it currently displayed while the part is selected. The script's parent local brick = script.Parent Go to Insert > Object > Script while the part is selected in order to insert a new script into it. Go to the script by locating it in Explorer and double clicking on its icon. Above is the first part of the script you will need. You can copy and paste it. The script's function function killbrick() Above here is the starting part of the function you'll need in order for this script to work. It is recommended that you use the same name for the function as the one above, but you can use your own. Copy and paste this under the part of the script that defines the script's parent, assuming you've copied and pasted it. Telling the script to find a humanoid local h = game.Workspace:findFirstChild("Humanoid") if h then h.Health = 0 end Now, what this does is, it defines a variable called "v", making it a function that tells the game to find an instance called "Humanoid", and if it has found "Humanoid", then you write the next part of the code below it. In "Humanoid" is a value named "Health" of which you can set to any value. Under that, write "end", because that is required for "if" statements. Telling the script to generate an explosion (and also to play an explosion sound, which is optional) local boom = Instance.new("Explosion") boom.Parent = brick local sound = brick.Sound sound:Play() This variable called "boom", generates an explosion. We'll also need to set the brick as its parent, otherwise it will be nonexistent, because it isn't in Workspace. This next part is optional, but let's assume for a moment here that it isn't. Insert a sound into the brick by going into Insert > Object > Sound. (Don't rename it unless you know what you are doing.) In the sound is a value called SoundId. Unless you want complete silence during your explosion, set this value to rbxasset://sounds/collide.wav. Telling the script to connect the function to its Touched value Hit Enter key and type "end" in the script. It's a very crucial element to functions, otherwise the script will be incomplete and not work. brick.Touched:connect(killbrick) This is an event in brick that we need to insert at the bottom of the script. This will tell the script to connect the Touched value/function in the brick to connect to the "killbrick" function, so that when the player steps upon the brick, it will execute the function we wrote in the script. Thus it will make the player explode and set their Health value to 0. The complete script If you weren't following the instructions correctly or you were just too impatient to read them, then here is the full script below. local brick = script.Parent function killbrick() local h = game.Workspace:findFirstChild("Humanoid") if h then h.Health = 0 end local boom = Instance.new("Explosion") boom.Parent = brick local sound = brick.Sound sound:Play() end brick.Touched:connect(killbrick) Category:Scripting tutorials